package com.zn.vo;


import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * @version 0.1
 * @time 12/26/17
 * @since 0.1
 */
public class Message<T> {
    public static final String SUCCESS = "200";

    public static final Message REQ_SUCCESS = new Message("200", "请求成功");
    public static final Message SYS_ERROR  = new Message("500", "网络繁忙,请稍后再试");
    public static final Message ARGS_ERROR  = new Message("400", "参数错误");
    public static final Message PHONE_NULL  = new Message("501", "手机号为空");
    public static final Message AGENCY_BUSINESS_CODE_IS_NULL  = new Message("502", "邀请码不存在");
    public static final Message VERIFICATION_CODE_INVALID = new Message("503", "验证码失效");
    public static final Message VERIFICATION_CODE_ERROR = new Message("504", "验证码错误");
    public static final Message REGISTER_SUCCESS=new Message("200","注册成功");
    public static final Message REGISTER_ERROR=new Message("505","注册失败");
    public static final Message CUSTOMER_EXIST=new Message("506","用户已存在");
    public static final Message ACCOUNT_PASSWORD_MISMATCH=new Message("507","账号密码不匹配");
    public static final Message TOKEN_NULL  = new Message("508", "token为空");
    public static final Message TOKEN_INVALID  = new Message("509", "token失效，请重新登录");
    public static final Message TOKEN_ATYPISM  = new Message("509", "token不一致，请重新登录");
    public static final Message LOGIN_OUT_ERROR  = new Message("510", "退出异常");
    public static final Message CUSTOMER_IS_DISABLE  = new Message("511", "该账户已被禁用");
    public static final Message CUSTOMER_IS_NOT_AUTHENTICATION  = new Message("512", "该账户未认证");
    public static final Message LACK_OF_FUNDS= new Message("513", "该账户可用资金不足");
    public static final Message NOT_TRADING_TIME= new Message("514", "当前非交易时间");
    public static final Message HOLD_VOLUME_INSUFFICIENT= new Message("515", "平仓手数不足");
    public static final Message REPORT_ORDER_NOT_EXIST= new Message("516", "响应报单不存在");
    public static final Message REPORT_ORDER_EXIST_PART= new Message("517", "响应报单存在部分成交");
    public static final Message CHANNEL_CAPITAL_INSUFFICIENT= new Message("518", "期权账户资金不足，请联系开户机构");
    public static final Message TIED_CARD_REPEAT= new Message("519", "该银行卡已绑定，不能重复绑定");
    public static final Message CUSTOMER_NOT_EXIST=new Message("520","用户不存在");
    public static final Message UPLOAD_FILE_ERROR=new Message("521","文件上传异常");
    public static final Message ACCOUNT_CAPITAL_PASSWORD_MISMATCH=new Message("522","账号与资金密码不匹配");
    public static final Message CAPITAL_PASSWORD_NULL=new Message("523","请去设置您的资金密码");
    public static final Message CONFIRM_PASSWORD_ATYPISM=new Message("524","确认密码不一致");
    public static final Message WITHDRAW_NUMBER_USE=new Message("525","当日提现次数已用完");
    public static final Message WITHDRAW_EXCEED_QUOTA=new Message("526","提现金额超出日累计限额");
    public static final Message SINGLE_MAXIMUM_QUOTA=new Message("527","金额超出单笔最高限额");
    public static final Message SINGLE_MINIMUM_QUOTA=new Message("528","金额小于单笔最低限额");
    public static final Message REPORT_ORDER_END= new Message("528", "响应报单已结束");
    public static final Message CUSTOMER_BANK_IS_NULL= new Message("529", "当前未绑卡");
    public static final Message CUSTOMER_BANK_NOT_FOUND=new Message("530","当前银行卡不存在");
    public static final Message PAY_ERROR=new Message("531","支付异常");
    public static final Message CONTRACE_ENTER_RISK_MANAGEMENT=new Message("532","该合约已进入风控等待平仓,不可下单");
    public static final Message WITHDRAW_EXIST_UNAUDITED=new Message("533","存在未审核提现申请");
    public static final Message CERTIFICATION_ERROR=new Message("534","客户认证失败");
    public static final Message CUSTOMER_ALREADY_AUTHENTICATION=new Message("534","客户已认证");
    public static final Message TRADE_PASSWORD_REGEX_ERROR=new Message("535","交易密码必须6-24位,数字+字母组合");
    public static final Message PHONE_NOT_REGISTER=new Message("536","该手机号未注册");
    public static final Message IN_CLOSE_TIME= new Message("536", "该合约已进入风控等待平仓,不可下单[536]");
    public static final Message SMS_ALL_TOO_OFTEN= new Message("537", "获取验证码过于频繁");
    public static final Message SMS_SEND_ERROR= new Message("538", "短信验证码发送异常");
    public static final Message CONTRACE_ALREADY_CHANGE=new Message("539","该合约已变更,无法跳转");
    public static final Message CUSTOMER_ALREADY_DISABLE=new Message("540","当前账户已禁用,请联系开户机构");
    public static final Message OLD_PASSWORD_INCORRECT=new Message("541","原密码不正确");
    public static final Message CONTRACT_ARGS_ERROR=new Message("542","合约参数异常,请联系开户机构");
    public static final Message SYSTEM_CONNECT_ERROR=new Message("542","连接异常,请稍后再试");
    public static final Message CUSTOMER_MATCHING_ERROR=new Message("543","平仓用户与开仓用户不匹配");
    public static final Message CONTRACECODE_MATCHING_ERROR=new Message("543","平仓合约与开仓合约不匹配");
    public static final Message STOPLOSSPOINT_IS_NULL=new Message("544","止损点数不能为0或空");
    public static final Message PHONE_NUM_ERROR=new Message("545","请输入正确的手机号");
    public static final Message BANK_NUM_ERROR=new Message("546","请输入正确的银行卡号");
    public static final Message ID_CARD_ERROR=new Message("546","请输入正确的身份证号");
    public static final Message RECHARGE_INSUFFICIENT_POUNDAGE=new Message("547","充值金额不足手续费");

    /**期权异常提示600+**/
    public static final Message TARGET_NOT_EXIST=new Message("600","当前标的不存在");
    public static final Message TRADE_ACCOUNT_ERROR=new Message("601","交易账户异常，请联系开户机构");
    public static final Message BUSINESS_LACK_OF_FUNDS=new Message("602","机构户资金不足,请联系开户机构");
    public static final Message UPDATE_BUSINESS_CAPITAL_FAIL=new Message("603","网络繁忙,请稍后再试[603]");
    public static final Message REPEAT_SUBMIT_ERROR=new Message("604","重复提交,请稍后再试");
    /**605支付桥异常**/
    public Message() {}
    public Message(String resCode, String msg) {
        this.resCode = resCode;
        this.msg = msg;
    }

    private String resCode = "200";

    private String msg;

    private T data;

    private Page page;

    public String getResCode() {
        return resCode;
    }

    public void setResCode(String resCode) {
        this.resCode = resCode;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public static <T> Message<T> successData(T data) {
        Message m = new Message<T>();
        m.setData(data);
        return m;
    }

    public static Message successMsg(String msg) {
        Message m = new Message();
        m.setMsg(msg);
        return m;
    }
    public static Message errorMsg(String msg) {
        Message m = new Message();
        m.setResCode("500");
        m.setMsg(msg);
        return m;
    }

    public static <T> Message page(Object list) {
        Message r = new Message();

        if (list instanceof com.github.pagehelper.Page) {
            com.github.pagehelper.Page page = (com.github.pagehelper.Page) list;
            Page p = new Page(page.getTotal(), page.getPageNum(),
                    page.getPageNum() == page.getPages() ? page.getPages() : page.getPageNum() + 1,
                    page.getPageNum() == 1 ? 1 : page.getPageNum() - 1,
                    page.getPageSize(), page.getPages());
            r.setPage(p);
            r.setData(page.getResult());
        } else {
            Page p;
            PageInfo pageInfo ;
            if (list instanceof PageInfo) {
                pageInfo = (PageInfo) list;
            } else {
                pageInfo = new PageInfo((List) list);
            }
            p = new Page(pageInfo.getTotal(), pageInfo.getPageNum(),
                    pageInfo.getPageNum() + 1, pageInfo.getPageNum() == 1 ? 1 : pageInfo.getPageNum() - 1,
                    pageInfo.getPageSize(), pageInfo.getPages());
            r.setPage(p);
            r.setData(pageInfo.getList());
        }
        return r;
    }
}
